<template>
  <div class="subject">
    <!-- 学科列表页面 TOP -->
    <div class="TOP">
      <el-card>
        <el-form :inline="true" :model="ject" class="demo-form-inline">
          <el-form-item label="学科编号">
            <el-input v-model="ject.rid"></el-input>
          </el-form-item>
          <el-form-item label="学科名称">
            <el-input v-model="ject.name"></el-input>
          </el-form-item>
          <el-form-item label="创建者">
            <el-input v-model="ject.username"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="ject.status" placeholder="请选择">
              <el-option label="禁用" value="0"></el-option>
              <el-option label="启用" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="seach">搜索</el-button>
            <el-button>清除</el-button>
            <el-button type="primary" @click="addSbuject">+新增学科</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <!-- 底部 -->
    <div class="bottom">
      <el-card>
        <el-table :data="jectList" style="width: 100%" ref="table">
          <el-table-column prop="id" label="序号" width="180">
            <template v-slot="{ $index }">
              {{ (page.page - 1) * page.pageSize + $index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="rid" label="学科编号" width="180">
          </el-table-column>
          <el-table-column prop="name" label="学科名称"> </el-table-column>
          <el-table-column prop="short_name" label="学科简称">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column prop="create_time" label="创建日期">
          </el-table-column>
          <el-table-column prop="status" label="状态">
            <template v-slot="{ row }">
              <span :class="{ red: row.status == '禁用' }">{{
                row.status
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template v-slot="{ row }">
              <el-button type="text" @click="edit(row)" size="small"
                >编辑</el-button
              >
              <el-button
                type="text"
                v-if="row.status === '启用'"
                size="small"
                @click="disable(row)"
                >禁用</el-button
              >
              <el-button type="text" v-else size="small" @click="start(row)"
                >启用</el-button
              >
              <el-button type="text" size="small" @click="del(row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页导航 -->
        <div style="text-align:center">
          <el-pagination
            @size-change="pageSize"
            @current-change="currentPage"
            :current-page="page.page"
            :page-sizes="[1, 5, 10, 100, 200, 300, 400]"
            :page-size="page.pageSize"
            :total="page.total"
            layout="total, sizes, prev, pager, next, jumper"
          >
          </el-pagination>
        </div>
      </el-card>
    </div>
    <!-- 新增学科 -->
    <Add ref="add" @getDate="getDate"></Add>
  </div>
</template>

<script>
import { subjectList, subjectRemove, subjectStatus } from '../../api/subject'
// import store from '../../store/index'
export default {
  components: {
    Add: () => import('./component/Dialog.vue')
  },
  data () {
    return {
      ject: {
        rid: '',
        name: '',
        username: '',
        status: ''
      },
      //   获取的列表数据
      jectList: [],
      //   分页导航绑定数据
      page: {
        //  总共多少条
        total: 400,
        // 默认第一页
        page: 1,
        // 页容量
        pageSize: 5
        //
      }
    }
  },
  methods: {
    // 搜索按钮
    async seach () {
      const res = await subjectList(this.ject)
      console.log(res)
      const getSearch = res.data.data.items
      getSearch.forEach(v => {
        if (v.status) {
          v.status = '启用'
        } else {
          v.status = '禁用'
        }
      })
      this.jectList = getSearch
      this.page.total = res.data.data.pagination.total
    },
    // 禁用按钮，设置学科的状态
    async disable (row) {
      await subjectStatus({
        id: row.id,
        status: 0
      })
      this.getDate()
      this.$message.success('状态修改成功')
      console.log(this.jectList)
    },
    // 启用按钮
    async start (row) {
      await subjectStatus({
        id: row.id,
        status: 1
      })
      this.getDate()
      this.$message.success('状态修改成功')
      console.log(this.$store.state.login.token)
    },
    // 删除学科
    del (row) {
      this.$confirm('您确定删除此学科码, 是否继续?', '提示', {
        confirmButtonText: '确定删除',
        cancelButtonText: '点错了',
        type: '1'
      })
        .then(async () => {
          await subjectRemove({ id: row.id })
          this.getDate()
          this.$message.success('删除')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑学科
    edit (row) {
      this.$refs.add.mode = 'edit'
      this.$refs.add.isShow = true
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      console.log(row)
    },
    // 新增学科
    addSbuject () {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'add'
    },
    // 页容量变动时执行
    pageSize (page) {
      this.page.pageSize = page
      this.getDate()
    },
    // 页码改变时触发
    currentPage (size) {
      this.page.page = size

      this.getDate()
    },
    // 获取列表数据
    async getDate () {
      console.log(this.$store.state.login.token)
      const res = await subjectList({
        page: this.page.page,
        limit: this.page.pageSize
      })
      console.log(res)
      this.jectList = res.data.data.items
      this.jectList.forEach(v => {
        if (v.status) {
          v.status = '启用'
        } else {
          v.status = '禁用'
        }
      })
      this.page.total = res.data.data.pagination.total
      console.log(this.$refs.table)
    }
  },
  created () {
    this.getDate()
  }
}
</script>
<style lang="less" scopen>
.subject {
  .red {
    color: red;
  }
  background-color: #cccccc;
  // .TOP {
  //   padding: 10px 10px 0 10px;
  // }
  // .bottom {
  //   padding: 0 10px 0 10px;
  //
  // }
}
</style>
